package org.sanm.domain.area;

import com.baomidou.mybatisplus.extension.toolkit.Db;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.sanm.common.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/area")
@Tag(name = "行政区域管理", description = "行政区域管理")
public class AreaController {

    @GetMapping("province/list")
    @Operation(summary = "省份列表", description = "省份列表")
    public Result<List<Province>> getProvinceList(){
        List<Province> list = Db.lambdaQuery(Province.class).list();
        return Result.success(list);
    }

    @GetMapping("city/list")
    @Operation(summary = "城市列表", description = "城市列表")
    public Result<List<City>> getCityList(@RequestParam(value = "provinceCode", required = false) String provinceCode){
        List<City> list = Db.lambdaQuery(City.class).eq(provinceCode != null, City::getProvinceCode, provinceCode).list();
        return Result.success(list);
    }

    @GetMapping("area/list")
    @Operation(summary = "区县列表", description = "区县列表")
    public  Result<List<Area>> getAreaList(
            @RequestParam(value = "provinceCode", required = false)
            String provinceCode,

            @RequestParam(value = "cityCode", required = false)
            String cityCode){
        List<Area> list = Db.lambdaQuery(Area.class)
                .eq(provinceCode != null, Area::getProvinceCode, provinceCode)
                .eq(cityCode != null, Area::getCityCode, cityCode)
                .list();
        return Result.success(list);
    }
}
